1327B - Princesses and Princes - CodeForces Solution


brute force graphs greedy *1200

Please click on ads to support us..

Python Code:

for i in range(int(input())):
    n = int(input())
    taken = [False for _ in range(n)]
    v = -1
    for j in range(n):
        l = [int(i)-1 for i in input().split()][1:]
        for k in l:
            if not taken[k]:
                taken[k] = True
                break
        else:
            v = j
    if v == -1:
        print('OPTIMAL')
    else:
        f = taken.index(False)
        print(f"IMPROVE\n{v+1} {f+1}")

C++ Code:

#include<bits/stdc++.h>
#include<bitset>
using namespace std;
typedef long long ll; 
const ll p=1e9+7;
// ll factorial(ll n)
// {
    // const ll M = 1000000007;
    // ll f = 1;
//  
    // for (ll i = 1; i <= n; i++)
       // f = (f*i) % M;
    // return f ;
// }
ll power(ll x,ll y,ll p)
{
    ll res = 1; 
 
    x = x % p; 
 
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1; 
        x = (x * x) % p;
    }
    return res;
}
// ll modInverse(ll n, ll p)
// {
    // return power(n, p - 2, p);
// }
// ll nCrModPFermat(ll n,ll r, ll p)
// {
    // if (n < r)
        // return 0;
    // if (r == 0)
        // return 1;
    // ll fac[n + 1];
    // fac[0] = 1;
    // for (ll i = 1; i <= n; i++)
        // fac[i] = (fac[i - 1] * i) % p;
//  
    // return (fac[n] * modInverse(fac[r], p) % p
            // * modInverse(fac[n - r], p) % p)
           // % p;
// }
void solve()
    { 
  ll n;
  cin>>n;
  ll N=n;
  vector<ll> d(n+1,0);
  vector<ll> p(n+1,0);
  ll j=1;
  while(N--)
  {
  	ll x;
  	cin>>x;
  	vector<ll> a(x);
  	ll c=0;
  	for(int i=0;i<x;i++)
  	{
  	  cin>>a[i];
  	  if(p[a[i]]==0 && c==0)
  	 {
  		p[a[i]]=1;
  		d[j]=1;
  		c++;
  	 }
  	}
  	j++;
  }
 for(int i=1;i<=n;i++)
 {
 	if(d[i]==0)
 	{
 		cout<<"IMPROVE"<<"\n";
 		cout<<i<<" ";
 		break;
 	}
 }
 for(int i=1;i<=n;i++)
 {
 	if(p[i]==0)
 	{
 		cout<<i<<" ";
 		return;
 	}
 }
 cout<<"OPTIMAL";
 
   }
 int main()
  { 
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    ll T = 1;
    cin >> T; 
    while (T--)
    {
        solve();
        cout<<"\n";
    }
    return 0;
  }


Comments

Submit
0 Comments
More Questions

1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String